package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.entity.AskersEntity;
import com.example.entity.StudentsEntity;
import com.example.service.AskersService;
import com.example.service.StudentsService;

@RequestMapping("/student")
@Controller
public class StudentsController {
	
	@Autowired
	private StudentsService studentService;
	@Autowired
	private AskersService askersService;
	
	/**
	 * 查询
	 * @param se
	 * @return
	 */
	@RequestMapping(path="/find",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> find(StudentsEntity se) {
		Map<String, Object> relMap=new HashMap<String, Object>();
		se.setOffset((se.getPage() - 1) * se.getLimit());
		List<StudentsEntity> list=studentService.find(se);
		int count=studentService.getCount(se);
		relMap.put("code", "0");
		relMap.put("msg", "success");
		relMap.put("count", count);
		relMap.put("data", list);
		return relMap;
	}
	
	/**
	 * 录入
	 * @param se
	 * @return
	 */
	@RequestMapping(path="/addStudents",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addStudents(StudentsEntity se){
		int count=studentService.addStudents(se);
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"录入成功":"录入失败");
		return relMap;
	}
	
	/**
	 * 修改
	 * @param se
	 * @param qq
	 * @return
	 */
	@RequestMapping(path="/upStudents",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upStudents(StudentsEntity se,String qq){
		se.setQQ(qq);
		int count=studentService.upStudents(se);
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"修改成功":"修改失败");
		return relMap;
	}
	
	/**
	 * 删除
	 * @param se
	 * @return
	 */
	@RequestMapping(path="/delStudents",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delStudents(StudentsEntity se){
		int count=studentService.delStudents(se.getId());
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"劝退成功":"劝退失败");
		return relMap;
	}
	
	/**
	 * 分配
	 * @param se
	 * @return
	 */
	@RequestMapping(path="/distribution",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> distribution(StudentsEntity se){
		List<AskersEntity> askerList = askersService.getTBConsultant();
		for (int i = 0; i < askerList.size(); i++) {
			if(se.getAskerid().equals(askerList.get(i).getAskerId()+"")) {
				se.setZiXunName(askerList.get(i).getAskerName());
			}
		}
		int count=studentService.distributionConsultants(se);
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"分配成功":"分配失败");
		return relMap;
	}
}
